package com.vito.oa.act.extend.action;

import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.act.model.ActExApproveInfoModel;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.extend.model.OaActExApproveInfoModel;
import com.vito.oa.act.extend.service.OaActExApproveInfoService;
/**
 * oa查询审批单附加摘要内容
 */
@Controller
@RequestMapping("oa/act/actExApproveInfo")
public class OaActExApproveInfoAction extends BaseAction{
	private static final Logger log = LoggerFactory.getLogger(OaActExApproveInfoAction.class);
	
	@Reference
	private OaActExApproveInfoService oaApproveService;
	
	/**
	 * 我发起的-流程中+已完结
	 * @param processKey
	 * @return
	 */
	@RequestMapping("queryLaunch")
	@ResponseBody
	public Result queryLaunch(OaActExApproveInfoModel actExApproveInfoModel){
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			actExApproveInfoModel.setUserid(user.getUserId());
			if(Validata.isNullOrEmpty(actExApproveInfoModel.getDataStatus())){
				actExApproveInfoModel.setDataStatus(OaActContants.PROCESS_DATA_STATUS_CONMON);
			}
			//默认取审批模块
			if(Validata.isNullOrEmpty(actExApproveInfoModel.getMoudelType())){
				actExApproveInfoModel.setMoudelType(OaActContants.PROCESS_MOUDEL_TYPE_APPROVAL);
			}
			if(!Validata.isNullOrEmpty(actExApproveInfoModel.getEndtime())){
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				actExApproveInfoModel.setEndtime(df.format(df.parse(actExApproveInfoModel.getEndtime()))+" 23:59:59");
			}
			r.setData(oaApproveService.queryOaLaunchByUserId(actExApproveInfoModel));
			r.setCode(Result.CODE_OK);
		}catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
		}
		return r;
	}
	/**
	 * 我审核的-流程中+已完结
	 * @param processKey
	 * @return
	 */
	@RequestMapping("queryApprove")
	@ResponseBody
	public Result queryApprove(OaActExApproveInfoModel actExApproveInfoModel){
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			actExApproveInfoModel.setUserid(user.getUserId());
			if(Validata.isNullOrEmpty(actExApproveInfoModel.getDataStatus())){
				actExApproveInfoModel.setDataStatus(OaActContants.PROCESS_DATA_STATUS_CONMON);
			}
			//默认取审批模块
			if(Validata.isNullOrEmpty(actExApproveInfoModel.getMoudelType())){
				actExApproveInfoModel.setMoudelType(OaActContants.PROCESS_MOUDEL_TYPE_APPROVAL);
			}
			r.setData(oaApproveService.queryOaApproveByUserId(actExApproveInfoModel));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
		}
		return r;
	}
	/**
	 * 我的根部门所有人发起的-流程中+已完结
	 * @param processKey
	 * @return
	 */
	@RequestMapping("queryLaunchByRootDept")
	@ResponseBody
	public Result queryLaunchByRootDept(OaActExApproveInfoModel actExApproveInfoModel){
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			if(Validata.isNullOrEmpty(actExApproveInfoModel.getDataStatus())){
				actExApproveInfoModel.setDataStatus(OaActContants.PROCESS_DATA_STATUS_CONMON);
			}
			//默认取审批模块
			if(Validata.isNullOrEmpty(actExApproveInfoModel.getMoudelType())){
				actExApproveInfoModel.setMoudelType(OaActContants.PROCESS_MOUDEL_TYPE_APPROVAL);
			}
			if(!Validata.isNullOrEmpty(actExApproveInfoModel.getEndtime())){
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				actExApproveInfoModel.setEndtime(df.format(df.parse(actExApproveInfoModel.getEndtime()))+" 23:59:59");
			}
			actExApproveInfoModel.setCreateDept(user.getDeptParentId());
			r.setData(oaApproveService.queryOaLaunchByRootDept(actExApproveInfoModel));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
		}
		return r;
	}
	
	/**
	 * 催办
	 * @return
	 */
	@RequestMapping("doSendMsg")
	@ResponseBody
	public Result doSendMsg(ActExApproveInfoModel actExApproveInfoModel){
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			actExApproveInfoModel.setUserid(user.getUserId());
			actExApproveInfoModel.setUsername(user.getUserName());
			oaApproveService.sendMsg(actExApproveInfoModel,user.getDeptParentId());
			r.setMsg("催办成功！");
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("催办异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg("催办失败，发生异常！");
		}
		return r;
	}
}